package com.test;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

public class Alternate_Exchange {

    public static void main(String[] args) throws
            IOException, TimeoutException, NoSuchAlgorithmException, KeyManagementException, URISyntaxException {

        // 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 主机地址: 默认为 localhost
        connectionFactory.setHost("localhost");
        // 连接端口: 默认为 5672
        connectionFactory.setPort(5672);
        // 虚拟主机名称: 默认为 /
        connectionFactory.setVirtualHost("/test");
        // 连接用户名: 默认为guest
        connectionFactory.setUsername("cfl");
        // 连接密码: 默认为guest
        connectionFactory.setPassword("cfl");
        // 从工厂获得连接：
        Connection connection = connectionFactory.newConnection();

        // 用连接创建Channel
        Channel channel = connection.createChannel();

        // 声明队列 普通交换机绑定的队列
        channel.queueDeclare("normalQueue", false, false, false, null);
        // 声明队列 alternate-exchange交换机绑定的队列
        channel.queueDeclare("unroutedQueue", false, false, false, null);

        /*
        AMQP.Exchange.DeclareOk exchangeDeclare(
            String exchange,
            BuiltinExchangeType type,
            boolean durable,
            boolean autoDelete,
            boolean internal,
            Map<String, Object> arguments) throws IOException;
        */
        // 定义参数
        Map<String, Object> arges = new HashMap<>();
        arges.put("alternate-exchange", "myAe");

        // 声明普通交换机 为DIRECT的交换机，交换机构造方法全部参数如上
        channel.exchangeDeclare("normal_Exchange", BuiltinExchangeType.DIRECT, false, false, arges);
        // 声明alternate-exchange交换机 为FANOUT的交换机，交换机构造方法全部参数如上
        channel.exchangeDeclare("myAe", BuiltinExchangeType.FANOUT, false, false, null);

        /*
        Queue.BindOk queueBind(
            String queue,
            String exchange,
            String routingKey,
            Map<String, Object> arguments
        ) throws IOException;
        */
        // 队列和普通交换机绑定，绑定key为rabbit
        channel.queueBind("normalQueue", "normal_Exchange", "rabbit", null);
        // 队列和备份交换机绑定 因为交换机类型是Fanout 绑定时不需要指定绑定key
        channel.queueBind("unroutedQueue", "myAe", "", null);

        String message1 = "test with rabbit true !!!";
        String message2 = "test with rabbit false !!!";
        /*
         * 参数1：指定交换机名称
         * 参数2：指定路由键routingKey
         * 参数3：指定扩展参数暂无
         * 参数4：指定发送的消息
         */
        // 分别指定routingKey
        channel.basicPublish("normal_Exchange", "rabbit", null, message1.getBytes());
        System.out.println(" [x] Sent '" + message1 + "'");

        channel.basicPublish("normal_Exchange", "nop", null, message2.getBytes());
        System.out.println(" [x] Sent '" + message2 + "'");

        // 关闭资源
        channel.close();
        connection.close();

    }

}
